package com.atguigu.crowd.service.impl;

import com.atguigu.crowd.entity.Admin;
import com.atguigu.crowd.entity.AdminExample;
import com.atguigu.crowd.exception.DeleteFailedException;
import com.atguigu.crowd.exception.LoginAcctAlreadyInUserException;
import com.atguigu.crowd.exception.LoginAcctAlreadyInUserForUpdateException;
import com.atguigu.crowd.exception.LoginFailedException;
import com.atguigu.crowd.mapper.AdminMapper;
import com.atguigu.crowd.service.api.AdminService;
import com.atguigu.crowd.constant.CrowdConstan;
import com.atguigu.crowd.util.CrowdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author zzqstart
 * @Description
 * @date 2022-08-28 1:42
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    private Logger logger = LoggerFactory.getLogger(AdminServiceImpl.class);

    @Override
    public void saveAdmin(Admin admin) {

        // 密码加密
        String passwordEncode = passwordEncoder.encode(admin.getUserPswd());

        admin.setUserPswd(passwordEncode);

        //创建时间
        String createTime = CrowdUtil.formatTime(new Date());
        admin.setCreateTime(createTime);

        try {
            adminMapper.insert(admin);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("异常全类名=" + e.getClass().getName());
            if (e instanceof DuplicateKeyException) {
                throw new LoginAcctAlreadyInUserException(CrowdConstan.MESSAGE_LGOIN_ACCT_ALREADY_IN_USE);
            }
        }
    }

    @Override
    public List<Admin> getAll() {
        List<Admin> adminList = adminMapper.selectByExample(new AdminExample());
        return adminList;
    }

    @Override
    public Admin getAdminByLoginAcct(String loginAcct) {

        // 1.在数据库中获取loginAcct对应的对象
        AdminExample example = new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andLoginAcctEqualTo(loginAcct);

        List<Admin> adminList = adminMapper.selectByExample(example);

        if (adminList == null || adminList.size() == 0) {
            throw new LoginFailedException(CrowdConstan.MESSAGE_LOGIN_FAILED);
        }

        if (adminList.size() > 1) {
            throw new RuntimeException(CrowdConstan.MESSAGE_SYSTEM_ERROR_LOGIN_NOT_UNIQU);
        }

        // 2.admin是否为空
        Admin admin = adminList.get(0);
        if (admin == null){
            throw new LoginFailedException(CrowdConstan.MESSAGE_LOGIN_NOEXIST_LOGINACCT);
        }

        return admin;
    }

    @Override
    public PageInfo<Admin> getAdminPage(String keyword, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);

        List<Admin> list = adminMapper.selectAdminByKeyword(keyword);

        PageInfo<Admin> adminPageInfo = new PageInfo<>(list);

        return adminPageInfo;
    }

    @Override
    public void remove(Integer adminId, Admin admin) {

        if (admin.getId() == adminId) {
            throw new DeleteFailedException(CrowdConstan.MESSAGE_DELETE_FAILED);
        }

        adminMapper.deleteByPrimaryKey(adminId);
    }


    @Override
    public Admin getAdminById(Integer adminId) {
        Admin admin = adminMapper.selectByPrimaryKey(adminId);
        return admin;
    }

    @Override
    public void updateAdmin(Admin admin) {

        try {
            adminMapper.updateByPrimaryKeySelective(admin);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("异常全类名=" + e.getClass().getName());
            if (e instanceof DuplicateKeyException) {
                throw new LoginAcctAlreadyInUserForUpdateException(CrowdConstan.MESSAGE_LGOIN_ACCT_ALREADY_IN_USE);
            }

        }
    }

}
